<template>
    <div class="login-page">
        <!--        logo-->
        <div class="login-heading">
            <div class="logo">
                <span class="logo-span" @click="$router.push('/')">芬 享</span>
                <span class="login-text">众享你的喜悦!</span>
            </div>
        </div>
        <el-row class="login-main">
            <!--            背景图-->
            <el-col :span="12">
                <img src="//s0.meituan.net/bs/file/?f=fe-sso-fs:build/page/static/banner/www.jpg"
                     alt="芬享网" width="480"
                     height="370">
            </el-col>
            <el-col :span="12">
                <!--        登录表单-->
                <el-form ref="form" :rules="rules" :model="form" label-width="80px" class="login-form">

                    <el-form-item label="账号" style="width: 400px" prop="account">
                        <el-input v-model="form.account" placeholder="输入账号"></el-input>
                    </el-form-item>

                    <el-form-item label="密码" style="width: 400px" prop="password">
                        <el-input type="password" show-password v-model="form.password" placeholder="输入密码"></el-input>
                    </el-form-item>

                    <el-form-item label="验证码" prop="code">
                        <div class="register-code-box">
                            <el-input v-model="form.code" class="code-input"></el-input>
                            <img class="code-img"
                                 @click="getCaptchaImage"
                                 :src="imgSrc" alt="点击刷新验证码">
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <div>
                            <el-button :loading="loading" @click="login" type="warning" style="width: 320px">
                                <span v-if="!loading">登 录</span>
                                <span v-else>登 录 中...</span>
                            </el-button>
                        </div>
                        <div class="go-register">
                            还没有账号？<span class="register-btn" @click="$router.push('/register')">免费注册</span>
                            <span class="forget-password">忘记密码？</span>
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-checkbox v-model="agreement" class="register-radio">
                            <div class="text"> 我已阅读并同意《芬享用户协议》《隐私政策》 ，
                                并授权芬享使用该芬享账号信息（如昵称、头像、个人信息等）进行统一管理
                            </div>
                        </el-checkbox>
                    </el-form-item>

                </el-form>

            </el-col>
        </el-row>
    </div>
</template>

<script>
    import {getCaptchaImage} from "@/api/common/captchaImage";

    export default {
        name: 'Login',
        data() {
            return {
                imgSrc: "",
                agreement: false,
                loading: false,
                form: {
                    account: "1111111111",
                    password: "123456",
                    code: "",
                    uuid: ""
                },
                rules: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                    code: [
                        {required: true, message: '请输入验证码', trigger: 'blur'},
                    ],
                },
            };
        },
        methods: {
            /**
             * 获取验证码
             */
            getCaptchaImage() {
                getCaptchaImage().then(res => {
                    this.form.uuid = res.uuid
                    this.imgSrc = "data:image/gif;base64," + res.img
                })
            },
            /**
             * 登录
             */
            login() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        /**
                         * 判断有没有勾选阅读事项
                         */
                        if (!this.agreement) {
                            this.msgWarn("您是否同意《服务协议》和《隐私政策》")
                            return;
                        }
                        this.loading = true
                        this.$store.dispatch("Login", this.form).then(() => {
                            let path = this.$route.query.continue_url
                            if (path != null && path !== '') {
                                location.href = path
                            } else {
                                location.href = '/'
                            }
                        }).catch(() => {
                            this.loading = false
                            this.getCaptchaImage()
                        })
                    } else {
                        return false
                    }
                })
            }
        },
        mounted() {
            this.getCaptchaImage()
        },
        components: {}
    }
</script>

<style scoped lang='less'>
    .login-page {
        .login-heading {
            height: 25vh;
            line-height: 25vh;
            padding: 0 10vw;

            .logo {
                .logo-span {
                    padding: 18px 8px;
                    background-color: rgb(255, 195, 0);
                    border-radius: 15px;
                    font-size: 20px;
                    font-weight: 600;
                    cursor: pointer;
                    margin-right: 10px;
                }

                .login-text {
                    color: #E6A23C;
                }
            }
        }

        .login-main {
            padding: 0 10vw;

            .login-form {

                .go-register {
                    color: #909399;

                    .register-btn {
                        color: #E6A23C;
                        cursor: pointer;
                    }

                    .forget-password {
                        color: #F56C6C;
                        cursor: pointer;
                        margin-left: 10px;
                    }
                }

                .register-code-box {
                    display: flex;
                    width: 480px;

                    .code-input {
                        width: 150px;
                    }

                    .code-img {
                        cursor: pointer;
                        margin-left: 10px;
                        height: 60px;
                        width: 160px;
                    }
                }


                .register-radio {
                    display: flex;

                    .text {
                        width: 320px;
                        line-height: 20px;
                        white-space: normal;
                        word-break: break-all;
                        word-wrap: break-word;
                    }
                }
            }
        }

    }
</style>